<?php
class TopNavWidget  extends CWidget{
    
	//输出字符串
	private $out = '';
    
    private $controller= '';
    
    private $action= '';

	public function init(){
        $this->controller = Yii::app()->controller->id;  
        $this->action = Yii::app()->controller->action->id; 
	}

	public function run(){
        $this->generateTopNav();
		echo $this->out;
	}
    
    public function configNavItem(){
        $item = array(
            array('title'=>'首页','url'=>Yii::app()->createUrl('site/index'),'active'=>($this->controller == 'site' ? 'active' : '')),
            array('title'=>'资源','url'=>Yii::app()->createUrl('resource/index'),'active'=>($this->controller == 'resource' ? 'active' : '')),
            array('title'=>'视频','url'=>Yii::app()->createUrl('video/index'),'active'=>($this->controller == 'video' ? 'active' : '')),
            );
        return $item;
    }
    
    public function generateTopNav(){
		$baseUrl = Yii::app()->baseUrl;
        $searchFormUrl = Yii::app()->createUrl('site/search');
        $url = yii::app()->createUrl('site/index');
		$login_url = Yii::app()->createUrl('admin/default/login');
        $item = $this->configNavItem();
        $itemString = '';
        $titleString = CHtml::encode(Yii::app()->name);
        $descriptionString = CHtml::encode(Yii::app()->params['description']);
        
        foreach ($item as $k=>$v){
            $itemString .= <<<ITEM
                    <li class="{$v['active']}">
                        <a href="{$v['url']}">
                        {$v['title']}
                        </a>
                    </li>
ITEM;
        }
        
        //判断是否重新打开页面进行跳转
        $redirectCode = '';
        if($this->controller == 'site' && $this->action == 'search'){
            $isRedirect = 'target="_blank"';
        }

        //登陆
        $login = Yii::app()->createUrl('/admin/login');

        $this->out .= <<<OUT
			<div class='navbar navbar-static-top' style='margin-bottom:20px'>
				<div class='navbar-inverse navbar-inner'>
					<ul class='nav pull-left'>
						<li class=''>
							<a href='/' target="_blank" class='brand' style='padding:10px'>
								<img src='/images/gowhich－logo.png' style='height:20px'/>
							</a>
						</li>
						
						{$itemString}
					</ul>
					<ul class='nav pull-right'>
						<li class=''>
							<a href='{$login_url}'>登陆</a>
						</li>
					</ul>
				</div>
			</div>
OUT;

    }
}


